﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic.Web;
using LotteryVote.Logic.Limit;
using LotteryVote.Utility;
using LotteryVote.Logic;
using LotteryVote.Logic.Exceptions;
using LotteryVote.Logic.Application;

namespace LotteryVote
{
    public partial class UpdateUpperLimit : CompanyPage
    {
        BetUpperLimitFacade upperLimitFacade = new BetUpperLimitFacade();
        int limitId;
        protected void Page_Load(object sender, EventArgs e)
        {
            Execute(() =>
            {
                InitParams();
            });
        }

        void InitParams()
        {
            if (string.IsNullOrEmpty(Request["LimitId"]) || !int.TryParse(Request["LimitId"], out limitId))
            {
                Response.Redirect("~/");
                return;
            }
            if (!IsPostBack)
            {
                var limit = upperLimitFacade.GetLimit(limitId);
                if (limit == null)
                    throw new InvalidDataException("不存在上限记录:UpperLimitId:" + limitId);
                txt_num.Text = limit.Num;
                txt_limit.Text = limit.UpperLlimit.ToString();
                var gameplayway = LotterySystem.Current.FindGamePlayWay(limit.GamePlayWayId);
                ltl_gameType.Text = string.Format("{0} {1}", gameplayway.GameType.GameName,
                    gameplayway.PlayWay == null ? string.Empty : GetGlobalResourceObject("Resource", gameplayway.PlayWay.WayName));
                var company = TodayLotteryCompany.Instance.GetTodayCompany().Find(it => it.CompanyId == limit.CompanyId);
                ltl_company.Text = company.Name;
            }
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            Execute(() =>
            {
                decimal amount;
                if (decimal.TryParse(txt_limit.Text, out amount))
                {
                    upperLimitFacade.UpdateUpperLimit(limitId, amount);
                    ShowMessage("Success", GetGlobalResourceObject("Resource", "Success").ToString());
                }
            });
        }
    }
}